package com.py.credit.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.py.credit.dao.BigScreenForBlackAndRedDao;
import com.py.credit.dao.DataMapCollectionDao;
import com.py.credit.service.BigScreenForBlackAndRedService;

@Service
public class BigScreenForBlackAndRedServiceImpl implements BigScreenForBlackAndRedService {

	@Autowired
	private BigScreenForBlackAndRedDao bigScreenForBlackAndRedDao;
	
	@Autowired
    private DataMapCollectionDao dataMapCollectionDao;

	@Override
	public Map<String, List> tendencyAnalyse(String type) {
		// TODO Auto-generated method stub
		List<Map> listO = bigScreenForBlackAndRedDao.tendencyAnalyse(type);
		List listMonth = new ArrayList();
		List listFr = new ArrayList();
		List listZrr = new ArrayList();

		for (int i = 0; i < listO.size(); i++) {
			Map m = listO.get(i);
			listMonth.add(m.get("all_month"));
			listFr.add(m.get("frcount"));
			listZrr.add(m.get("zrrcount"));
		}
		Map m = new HashMap();
		m.put("listMonth", listMonth);
		m.put("listFr", listFr);
		m.put("listZrr", listZrr);
		return m;
	}

	@Override
	public Map getPie(String type) {
		Map m = new HashMap();
		long frcount = 0;
		long zrrcount = 0;
		long allcount = 0;
		if (type != null && !"".equals(type)) {
			if ("red".equals(type)) {
				frcount = bigScreenForBlackAndRedDao.getCount("t_1000000010005_JgddEske8s");
				zrrcount = bigScreenForBlackAndRedDao.getCount("t_1000010010006_5qTzTGI6gL");
			} else {
				frcount = bigScreenForBlackAndRedDao.getCount("t_1000000010007_u6tnVOcB9I");
				zrrcount = bigScreenForBlackAndRedDao.getCount("t_1000010010008_VU2pjsxdhk");
			}
		}
		allcount = frcount + zrrcount;
		m.put("frcount", frcount);
		m.put("zrrcount", zrrcount);
		m.put("allcount", allcount);
		// TODO Auto-generated method stub
		return m;
	}

	@Override
	public List<Object[]> countByDept(String type) {
		// TODO Auto-generated method stub
		return bigScreenForBlackAndRedDao.countByDept(type);
	}

	@Override
	public List<Map> getIndByZone(String type) {
		// TODO Auto-generated method stub
		List<Map> countAll = bigScreenForBlackAndRedDao.getIndByZone(type);
		List<Map> thisMonthList = bigScreenForBlackAndRedDao.getCountByZoneThisMonth(type);
		List<Map> lastMonthList = bigScreenForBlackAndRedDao.getCountByZoneLastMonth(type);
		List<Map> all = new ArrayList<Map>();
		for (int i = 0; i < countAll.size(); i++) {
			Map m = countAll.get(i);
			long thisMonth = ((BigDecimal) thisMonthList.get(i).get("sumCount")).longValue();
			long lastMonth = ((BigDecimal) lastMonthList.get(i).get("sumCount")).longValue();
			double rate;
			if (lastMonth == 0 && thisMonth != 0) {
				rate = 100;
			} else if (lastMonth == 0 && thisMonth == 0) {
				rate = 0;
			} else {
				rate = (double) (thisMonth - lastMonth) / lastMonth;
				rate = new BigDecimal(rate).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			}
			m.put("rate", rate);
		}
		return countAll;
	}

	@Override
	public List<Object[]> getTopEnterprise(String type, String qybm) {
		// TODO Auto-generated method stub
		return bigScreenForBlackAndRedDao.getTopEnterprise(type, qybm);
	}

	@Override
	public List<Map> countByHybm(String type, String qybm) {
		// TODO Auto-generated method stub
		return bigScreenForBlackAndRedDao.countByHybm(type, qybm);
	}
	
	@Override
	public Map<String,List<Map>> getRecentDataByDept() {
		// TODO Auto-generated method stub
		List<LinkedHashMap> deptList=dataMapCollectionDao.selectDeptList();
		Map<String,List<Map>> m=new HashMap<String,List<Map>>();
		for (LinkedHashMap dept:deptList) {
			m.put(dept.get("officeName").toString(), bigScreenForBlackAndRedDao.getRecentDataByDept(dept.get("deptId").toString()));
		}
		return m;
	}

}
